import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/models/message_list_entity.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/widget/null_tip.dart';

import '../../../constants.dart';
import 'logic.dart';

class MessagePage extends StatelessWidget {
  MessagePage({Key? key}) : super(key: key);

  final logic = Get.find<MessageLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        title: Text(
          '消息',
          style: TextStyle(fontSize: kFontSizeMedium),
        ),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back(result: logic.unreadCount);
          },
        ),
        bottom: TabBar(
          controller: logic.tabController,
          tabs: logic.tabTitles
              .map((e) => Tab(
                    child: FittedBox(
                      fit: BoxFit.scaleDown,
                      child: Text(e),
                    ),
                  ))
              .toList(),
        ),
      ),
      body: _buildListItemBg(),
    );
  }

  List<Widget> _itemBuilder(MessageData item) {
    return [
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
            child: Text(
              item.title,
              style: TextStyle(
                fontSize: kFontSizeSmall,
                fontWeight: AppFont.fontSemiBold,
                color: item.readStatus == 1
                    ? AppColor.colorFontGrey
                    : AppColor.colorFontBlack,
              ),
            ),
          ),
          SizedBox(width: kSpaceSmall),
          Text(
            DateFormat('yyyy.MM.dd HH:mm')
                .format(DateTime.fromMillisecondsSinceEpoch(item.createdAt)),
            style: TextStyle(
              color: AppColor.colorFontGrey,
              fontSize: kFontSizeMini,
            ),
          ),
        ],
      ),
      SizedBox(height: kSpaceSmall),
      Text(
        item.content,
        style: TextStyle(
          fontSize: kFontSizeSmall,
          color: item.readStatus == 1
              ? AppColor.colorFontGrey
              : AppColor.colorFontBlack,
        ),
      ),
    ];
  }

  Widget _renderRow(BuildContext context, int index) {
    if (index < logic.messageList.length) {
      return Container(
        margin: EdgeInsets.only(
            left: kSpaceMedium, right: kSpaceMedium, top: kSpaceMedium),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(kRadiusMedium),
        ),
        padding: EdgeInsets.all(kSpaceSmall),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: _itemBuilder(logic.messageList[index]),
        ),
      );
    } else {
      return _getMoreWidget();
    }
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(kSpaceMedium),
        child: Text(
          logic.bottomTips.value,
          style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey),
        ),
      ),
    );
  }

  Widget _buildListItemBg() {
    return Obx(() {
      if (logic.isLoading.value) {
        return const Center(child: CircularProgressIndicator());
      } else if (logic.messageList.isEmpty) {
        return const NullTip();
      } else {
        return ListView.builder(
          itemBuilder: _renderRow,
          itemCount: logic.messageList.length + 1,
          controller: logic.scrollController,
        );
      }
    });
  }
}
